package com.wq.sofa.framework.bolt.client;

import com.alipay.remoting.ConnectionEventType;
import com.alipay.remoting.InvokeCallback;
import com.alipay.remoting.exception.RemotingException;
import com.alipay.remoting.rpc.RpcClient;
import com.alipay.remoting.rpc.RpcResponseFuture;
import com.wq.sofa.framework.bolt.BoltCommonHelper;
import com.wq.sofa.framework.bolt.ConnectEventProcessor;
import com.wq.sofa.framework.bolt.DisConnectEventProcessor;
import com.wq.sofa.framework.bolt.model.CommonRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @Author: wangqiang20995
 * @Date:2018/9/19
 * @Description:
 * @Resource:
 */
public class SimpleClient {

    private RpcClient rpcClient;

    private Logger logger = LoggerFactory.getLogger(getClass());

    private String host;

    private int port;

    private String address;


    public SimpleClient(String host, int port){
        this.host = host;
        this.port = port;
        this.rpcClient = new RpcClient();

        rpcClient.addConnectionEventProcessor(ConnectionEventType.CONNECT,new ConnectEventProcessor());
        rpcClient.addConnectionEventProcessor(ConnectionEventType.CLOSE,new DisConnectEventProcessor());
        rpcClient.registerUserProcessor(new SimpleOSProcessor());
        this.address = this.host + ":" + this.port;
        rpcClient.init();
    }

    public Object sendMessageSyn(byte code,String system){
        CommonRequest commonRequest = commonRequest(code, system);
        logger.info("prepare send message --> {}",commonRequest);
        Object response = null;
        try {
            response = this.rpcClient.invokeSync(this.address,commonRequest,10000);
            logger.info("receive server response --> [{}]",response);
        } catch (RemotingException e) {
            logger.error(e.getMessage(),e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(),e);
        }

        if(response == null) {
            response = "empty object";
        }
        return  response;
    }

    private CommonRequest commonRequest(byte code, String system) {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setCode(code);
        commonRequest.setSystem(system);
        commonRequest.setRequestId(BoltCommonHelper.generatorUUID());
        return commonRequest;
    }

    public RpcResponseFuture sendMessageAndReceiveFuture(byte code,String system){
        CommonRequest commonRequest = commonRequest(code, system);
        logger.info("future model send message --> [{}]",commonRequest);

        try {
            return this.rpcClient.invokeWithFuture(this.address,commonRequest,null,10000);
        } catch (RemotingException e) {
            logger.error(e.getMessage(),e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(),e);
        }

        return null;
    }

    public void sendMessageWithCallBack(byte code, String system, InvokeCallback invokeCallback){
        CommonRequest commonRequest = commonRequest(code,system);
        logger.info("callback model and send message --> [{}]",commonRequest);

        try {
            this.rpcClient.invokeWithCallback(this.address,commonRequest,invokeCallback,10000);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }


}
